brute force constructive algorithms implementation *1600

Please click on ads to support us..

Python Code:

from string import digits
  
chosen = "xxxx"

f_bulls = []
  
my_dict = {}
for i in digits:
  my_dict.update({i:{
    "excepect":[]
  }})
  
def make_chosen(chosen,obj=None):
  if type(obj) != int:
    return ''.join(list(map(lambda x: "x" if x[0] not in obj else x[1], enumerate(chosen))))
  else:
    return ''.join(list(map(lambda x: "x" if x[0] == obj else x[1], enumerate(chosen))))
  
def int_input():
  return list(map(lambda a: int(a), input().split(" ")))
  
def guess(chosen: str, old=None,my_digits = digits[5:] + digits[:5]):
  for i in my_digits:
    if chosen.count("x") == 0: break
    place = chosen.index("x")     if place not in my_dict[i]["excepect"] and i != old and i not in chosen:
      chosen = chosen.replace("x",i,1)
  if chosen.count("x") > 0:
    chosen = chosen.replace("x",chosen[f_bulls[0]])
  return chosen
  
def sure_bull(b1, chosen, f_bull: list):  
  copy = chosen
  bulls = f_bull
  i = 0
  while i < 4:
    if i in f_bulls:
      i += 1
      continue
    chosen = make_chosen(chosen,i)
    chosen = guess(chosen , copy[i])
    print(chosen)
    [b,_] = int_input()
    if b == 4:
      return "Out",[0,1,2,3]
    if b < b1:
      bulls.append(i)
  
    i += 1
    chosen = copy
  return chosen,bulls
  
b_old, c_old = 0, 0
while b_old != 4:
  chosen = guess(chosen)
  print(chosen)
  [b_new, c_new] = int_input()
  if b_new == 4: break
  if b_new == 0 or b_new <= b_old:
    for i,val in enumerate(chosen):
      if i not in f_bulls:
        my_dict[val]["excepect"].append(i)
  if b_new > b_old:
    chosen,bulls = sure_bull(b_new, chosen, f_bulls)
    if chosen == "Out": break
    f_bulls.extend(bulls)
    f_bulls = list(set(f_bulls))
    chosen = make_chosen(chosen,f_bulls)
    b_old, c_old = b_new, c_new
  else:
    chosen = make_chosen(chosen,f_bulls)


Comments

Submit
0 Comments
More Questions

987A - Infinity Gauntlet
1676G - White-Black Balanced Subtrees
1716D - Chip Move
1352F - Binary String Reconstruction
1487B - Cat Cycle
1679C - Rooks Defenders
56A - Bar
1694B - Paranoid String
35A - Shell Game
1684A - Digit Minimization
43B - Letter
1017A - The Rank
1698B - Rising Sand
235A - LCM Challenge
1075B - Taxi drivers and Lyft
1562A - The Miracle and the Sleeper
1216A - Prefixes
1490C - Sum of Cubes
868A - Bark to Unlock
873B - Balanced Substring
1401D - Maximum Distributed Tree
1716C - Robot in a Hallway
1688B - Patchouli's Magical Talisman
99A - Help Far Away Kingdom
622B - The Time
1688C - Manipulating History
1169D - Good Triple
1675B - Make It Increasing
588A - Duff and Meat
1541B - Pleasant Pairs